//
//  SCTravelViewController.m
//  LastOne
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCTravelViewController.h"
#import "FitHeader.h"
#import "SCButtonTraverCollectionReusableView.h"
#import "SCTitleLabelCollectionReusableView.h"
#import "SCHotTravelCollectionViewCell.h"
#import "SCOtherTravelCollectionViewCell.h"
#import "HTTPMethod.h"
#import "SCDesCity.h"
#import "SCCountryViewController.h"
#import "NightHeader.h"
#import "GiFHUD.h"
#import "FitHeader.h"
@interface SCTravelViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, assign)NSInteger flag;
@end

@implementation SCTravelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    //加载页面
    [GiFHUD setGifWithImageName:@"loading.gif"];
    _flag = 0;
    self.navigationItem.title = @"爱游";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(5 * FitWidth, 10 * FitHeight, 5 * FitWidth, 10 * FitHeight);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    [_collectionView NightWithType:UIViewColorType2];
    //注册分区头视图(button点击)
    [_collectionView registerClass:[SCButtonTraverCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"buttonHeader"];
    //注册分区头视图
    [_collectionView registerClass:[SCTitleLabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"titleHeader"];
    //注册热门旅游Cell
    [_collectionView registerClass:[SCHotTravelCollectionViewCell class] forCellWithReuseIdentifier:@"HotTravelCell"];
    //注册其他旅游cell
    [_collectionView registerClass:[SCOtherTravelCollectionViewCell class] forCellWithReuseIdentifier:@"OtherTrallCell"];
    [self.view addSubview:_collectionView];
    
}
//获取数据
- (void)getData
{
    [GiFHUD show];
    _dataSourceArray = [NSMutableArray array];
    [HTTPMethod getDataByString:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=861138023945773&track_app_version=6.7&track_app_channel=360m&track_device_info=mx3&track_os=Android4.2.1&app_installtime=1443017336409&lat=38.883605&lon=121.544473" BodyDic:nil WithDataBlock:^(id data) {
       
        NSArray *array = [data objectForKey:@"data"];
        [_dataSourceArray addObjectsFromArray:array];
        
        [_collectionView reloadData];
        [GiFHUD dismiss];
    }];
}
#pragma mark - collectionView代理方法

//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (_dataSourceArray.count == 0) {
        return 0;
    }
    if (_flag == 6) {
        return 1;
    }
    return 2;
}

//每个分区的item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_dataSourceArray.count == 0) {
        return 0;
    }
    if (section == 0) {
        return [[_dataSourceArray[_flag] objectForKey:@"hot_country"] count];
    }
    else{
        return [[_dataSourceArray[_flag] objectForKey:@"country"] count];
    }
}

//
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //热门旅游
    if (indexPath.section == 0) {
        SCHotTravelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotTravelCell" forIndexPath:indexPath];
        SCDesCity *city = [[SCDesCity alloc] init];
        [city setValuesForKeysWithDictionary:[[_dataSourceArray[_flag] objectForKey:@"hot_country"] objectAtIndex:indexPath.row]];
        cell.city = city;
        
        return cell;
    }
    //其他旅游
    else{
        SCOtherTravelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OtherTrallCell" forIndexPath:indexPath];
        SCDesCity *city = [[SCDesCity alloc] init];
        [city setValuesForKeysWithDictionary:[[_dataSourceArray[_flag] objectForKey:@"country"] objectAtIndex:indexPath.row]];
       // cell.backgroundColor = [UIColor yellowColor];
        cell.city = city;
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
}


//每个cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //热门旅游
    if (indexPath.section == 0) {
        return CGSizeMake(170 * FitWidth, 250 * FitHeight);
    }
    //其他旅游
    else{
        return CGSizeMake(170 * FitWidth, 60 * FitHeight);
    }
}

//头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        SCButtonTraverCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"buttonHeader" forIndexPath:indexPath];
        reusableView.label.text = [NSString stringWithFormat:@"%@热门目的地", [_dataSourceArray[_flag] objectForKey:@"cnname"]];
        
        //匿名函数实现体:即block内部
        void(^SelectButtonNumberBlock)(NSInteger) = ^(NSInteger buttonNumber){
            //使用传递过来的值
           _flag = buttonNumber;
        [_collectionView reloadData];
        };
        reusableView.block = SelectButtonNumberBlock;
        return reusableView;
    }
    //标题分区视图
    else{
        SCTitleLabelCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"titleHeader" forIndexPath:indexPath];
        reusableView.label.text = [NSString stringWithFormat:@"%@其他目的地", [_dataSourceArray[_flag] objectForKey:@"cnname"]];

        return reusableView;
    }
}

//分区视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(self.view.frame.size.width, 300 * FitHeight);
    }
    else{
        return CGSizeMake(self.view.frame.size.width, 40 * FitHeight);
    }
}

//点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *coutryID;
    if (indexPath.section == 0) {
        coutryID = [[[_dataSourceArray[_flag] objectForKey:@"hot_country"] objectAtIndex:indexPath.row] objectForKey:@"id"];
    }
    else{
        coutryID = [[[_dataSourceArray[_flag] objectForKey:@"country"] objectAtIndex:indexPath.row] objectForKey:@"id"];
    }
    SCCountryViewController *scdVC = [[SCCountryViewController alloc] init];
    scdVC.countryId = coutryID;
    [self.navigationController pushViewController:scdVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
